﻿using Autofac;
using Microsoft.Extensions.Configuration;
using Surging.Core.Caching.Configurations;
using Surging.Core.Consul;
using Surging.Core.Consul.Configurations;
using Surging.Core.CPlatform;
using Surging.Core.CPlatform.Configurations;
using Surging.Core.CPlatform.Utilities;
using Surging.Core.EventBusRabbitMQ.Configurations;
using Surging.Core.ProxyGenerator;
using Surging.Core.ServiceHosting.Internal;

namespace Surging.Base
{
    public static class SurgingExtension
    {
        public static IServiceHostBuilder RegisterMicroServices(this IServiceHostBuilder builder)
        {
            builder.RegisterServices(mybuilder =>
            {
                mybuilder.AddMicroService(option =>
                {
                    option.AddServiceRuntime()
                        .AddRelateService()
                        .AddConfigurationWatch()
                        .UseConsulManager(new ConfigInfo(Core.CPlatform.AppConfig.GetSection("Consul")?.Get<ConsulOption>().ConnectionString))
                        .AddServiceEngine(typeof(SurgingServiceEngine));
                    mybuilder.Register(p => new CPlatformContainer(ServiceLocator.Current)); //初始化注入容器
                });
            });
            return builder;
        }

        public static IServiceHostBuilder RegConfigure(this IServiceHostBuilder builder,bool regCache, bool regEvent)
        {
            builder.Configure(x =>
            {
                x.AddCPlatformFile("Configs/surgingSettings.json", optional: false, reloadOnChange: true);
                if (regCache)
                {
                    x.AddCacheFile("Configs/cacheSettings.json", optional: false, reloadOnChange: true);
                }
                if (regEvent)
                {
                    x.AddEventBusFile("Configs/eventBusSettings.json", optional: false);
                }
            });
            return builder;
        }
    }
}
